<template>
    <div style="min-width:1042px">
       <div v-if="$route.path == '/personalAdmin/newStatistics'">
            <div class="vs-search-box" style="padding: 24px 22px 0 0px;">
                <div class="statistics">
                    <el-switch
                        @change="init"
                        v-model="statistics"
                        active-text="按月统计"
                        inactive-text="按日统计">
                    </el-switch>
                </div>
                <el-form ref="form" :inline="true" :model="paging" label-width="90px">
                    <el-form-item label="门店">
                        <el-select v-model="paging.store_id" @change="staffshiftType" style="width:240px" placeholder="请选择">
                            <el-option
                                v-for="item in shopList"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="日期" >
                        <el-date-picker
                        v-model="paging.date"
                        type="month"
                        value-format="yyyy-MM"
                        v-show="statistics"
                        style="width:240px"
                        :clearable="false"
                        placeholder="选择月">
                        </el-date-picker>
                        <el-date-picker
                        v-model="paging.start_time"
                        type="daterange"
                        v-show="!statistics"
                        :clearable="false"
                        style="width:240px"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                        </el-date-picker>
                        <!-- <el-select v-model="paging.type" style="width:240px" placeholder="请选择">
                            <el-option label="调动" :value="1"> </el-option>
                            <el-option label="离职" :value="2"> </el-option>
                        </el-select> -->
                    </el-form-item>
                    <el-form-item label="" class="fl-right">
                        <el-button @click="init" >重置</el-button>
                        <el-button @click="handleCurrentChange(1)" type="primary">查询</el-button>
                    </el-form-item>
                    <el-form-item label="校准状态">
                        <el-select v-model="paging.abnormal" style="width:240px" placeholder="请选择">
                            <el-option label="正常" :value="1"> </el-option>
                            <el-option label="异常" :value="0"> </el-option>
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item label="姓名">
                        <el-input style="width:240px" v-model="paging.name"></el-input>
                    </el-form-item>
                    <el-form-item label="工号">
                        <el-input style="width:240px" v-model="paging.number"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位">
                       <el-select v-model="paging.position" style="width:240px" placeholder="请选择">
                           <el-option
                                v-for="item in positionList"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="班次">
                       <el-select v-model="paging.shift_type" style="width:240px" placeholder="请选择">
                            <el-option
                                v-for="item in classesList"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="请假申请">
                       <el-select v-model="paging.leave_type_id" style="width:240px" placeholder="请选择">
                            <el-option
                                v-for="item in leaveTypeList"
                                :key="item"
                                :label="leaveTypeList[item]"
                                :value="item"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <div v-show="statistics" class="vs-table-box">
                <div class="operation-box">
                    <el-button class="left-auto" @click="derive2" :loading="isDerive2" icon="el-icon-download" size="medium" >导出</el-button>
                </div>
                <el-table 
                :header-cell-style="{background:'#f5f6f8'}" 
                v-loading="loading" 
                :data="monthList" 
                header-row-class-name="has-gutter-table" 
                style="width: 100%">
                    <el-table-column prop="date" label="日期" ></el-table-column>
                    <el-table-column prop="avatar" label="头像" width="80" >
                        <template slot-scope="scope">
                            <el-image  
                            class="image-thumbnail" 
                            :src="scope.row.avatar" 
                            style="width:60px;height:60px"
                            fit="cover" 
                            :preview-src-list="[scope.row.avatar]" >
                            </el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" ></el-table-column>
                    <el-table-column prop="number" label="工号" ></el-table-column>
                    <el-table-column prop="store.title" label="门店"></el-table-column>
                    <el-table-column prop="position.title" label="岗位"></el-table-column>
                    <el-table-column prop="nationality" label="国籍">
                        <template slot-scope="scope">
                            {{scope.row.nationality==1?'中国':'泰国'}}
                        </template>
                    </el-table-column>
                    <el-table-column label="概况" align="center">
                        <el-table-column prop="answer_clock_num" label="应打卡天数" ></el-table-column>
                        <el-table-column prop="normal_clock_num" label="正常打卡天数" ></el-table-column>
                        <el-table-column prop="abnormal_clock_num" label="异常天数" ></el-table-column>
                        <el-table-column prop="leave_num" label="请假天数" ></el-table-column>
                    </el-table-column>
                    <el-table-column label="异常统计" align="center">
                        <el-table-column prop="late_num" label="迟到次数" ></el-table-column>
                        <el-table-column prop="late_time" label="迟到时长" ></el-table-column>
                        <el-table-column prop="out_num" label="早退次数" ></el-table-column>
                        <el-table-column prop="out_time" label="早退时长" ></el-table-column>
                        <el-table-column prop="missing_card_num" label="缺卡次数" ></el-table-column>
                        <el-table-column prop="absenteeism_num" label="旷工天数" ></el-table-column>
                    </el-table-column>

                    <el-table-column label="请假统计" align="center">
                        <el-table-column prop="compassionate_leave_day" label="事假" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.compassionate_leave_day">
                                    {{ scope.row.compassionate_leave_day }}天数
                                </span>
                                {{ scope.row.compassionate_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="病假" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.sick_leave_day">
                                    {{ scope.row.sick_leave_day }}天数
                                </span>
                                {{ scope.row.compassionate_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="婚假" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.marriage_leave_day">
                                    {{ scope.row.marriage_leave_day }}天数
                                </span>
                                {{ scope.row.marriage_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="产假" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.aternity_leave_day">
                                    {{ scope.row.aternity_leave_day }}天数
                                </span>
                                {{ scope.row.aternity_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="陪产假" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.paternity_leave_day">
                                    {{ scope.row.paternity_leave_day }}天数
                                </span>
                                {{ scope.row.paternity_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="其他" >
                            <template slot-scope="scope">
                                <span v-if="scope.row.other_leave_day">{{ scope.row.other_leave_day }}天数</span>
                                {{ scope.row.other_leave_time | timeNum }}
                            </template>
                        </el-table-column>
                    </el-table-column>
                </el-table>
            </div>
            <div v-show="!statistics" class="vs-table-box">
                <div class="operation-box">
                    <el-button class="left-auto" @click="derive" :loading="isDerive" icon="el-icon-download" size="medium" >导出</el-button>
                </div>
                <el-table 
                :header-cell-style="{background:'#f5f6f8'}" 
                v-loading="loading" 
                :data="dayList" 
                header-row-class-name="has-gutter-table" 
                style="width: 100%">
                    <el-table-column prop="time" label="日期" ></el-table-column>
                    <el-table-column prop="avatar" label="头像" width="80" >
                        <template slot-scope="scope">
                            <el-image  
                            class="image-thumbnail" 
                            :src="scope.row.avatar" 
                            fit="cover" 
                            style="width:70px;height:70px"
                            :preview-src-list="[scope.row.avatar]" >
                            </el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" ></el-table-column>
                    <el-table-column prop="number" label="工号" ></el-table-column>
                    <el-table-column prop="store_title" label="门店"></el-table-column>
                    <el-table-column prop="position_title" label="岗位"></el-table-column>
                    <el-table-column prop="nationality" label="国籍">
                        <template slot-scope="scope">
                            {{scope.row.nationality==1?'中国':'泰国'}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="nationality" label="国籍">
                        <template slot-scope="scope">
                            {{scope.row.nationality==1?'中国':'泰国'}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="shift_start_time" label="上班打卡时间"></el-table-column>
                    <el-table-column prop="shift_end_time" label="下班打卡时间"></el-table-column>
                    <el-table-column prop="position_title" label="请假申请">
                        <template slot-scope="scope" v-if="scope.row.staff_leave">
                            {{scope.row.staff_leave | leaveType}} {{scope.row.leave_start_time}}-{{scope.row.leave_end_time}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="is_abnormal" label="校准状态">
                        <template slot-scope="scope">
                            {{scope.row.is_abnormal ==1 ?'正常':'异常'}}
                        </template>
                    </el-table-column>
                    <!-- 有请假显示请假  旷工late_time 迟到-->
                    <el-table-column prop="is_abnormal" align="center" label="异常情况">
                        <template slot-scope="scope">
                            {{scope.row | leaveAnomaly}}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="vs-paging-box">
                    <el-pagination
                        @current-change="handleCurrentChange"
                        :current-page.sync="paging.page"
                        :page-size="paging.limit"
                        layout="prev, pager, next, jumper"
                        :total="paging.total">
                    </el-pagination>
                </div>
            </div>

       </div>
       <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            statistics:true,
            paging:{
                store_id:'',//门店ID
                name:'',//姓名
                number:'',//工号
                start_time:'',//开始时间
                end_time:'',//结束时间
                date:'',//时间 yyyy-MM
                abnormal:'',//校准状态
                position:'',//岗位ID
                shift_type:'',//排班类型
                leave_type_id:'',//请假类型
                page:1,
                total:0,
                limit:10,
            },
            today:'',
            loading:false,
            shopList:[],
            dayList:[],
            monthList:[],
            positionList:[],
            classesList:[],
            leaveTypeList:[],
            tableData: [],
            isDerive:false,
            isDerive2:false
        };
    },
    created() {
        let date = new Date();
        this.today = `${date.getFullYear()}-${date.getMonth()+1}`
        this.paging.date = this.today;
        this.homegetStore();
        this.homegetPosition();
        this.staffshiftType();
        this.getleaveType();
        this.getList();
    },
    filters:{
        leaveAnomaly(val){
            if(val.staff_leave){
                return '请假 '
            } else if(val.is_absenteeism){
                return `旷工 ${val.late_time?val.late_time+'分钟':''}`
            } else if(val.is_late){
                return `迟到 ${val.late_time}分钟`
            } else if(val.is_out){
                return `早退 ${val.out_time}分钟`
            }
            return '--';
        },
        timeNum(val){
            if(!val)return '';
            let day = Number.parseInt(val/60);
            let time = val%60;
            return (day?day+'小时':'') +(time?time+'分钟':'')
        }
    },
    methods: {
        async derive(){
            this.isDerive = true;
            let res = await this.$Http.attendanceStatisticsExport(this.paging);
            this.utils.downloadXlsx(res,'考勤日统计')
            this.isDerive = false;
        },
        async derive2(){
            this.isDerive2 = true;
            let res = await this.$Http.attendanceStatisticsMonthExport(this.paging);
            this.utils.downloadXlsx(res,'考勤月统计')
            this.isDerive2 = false;
        },
        statChange(e){//

        },
        init(val){
            this.paging = this.$options.data().paging;
            this.paging.date = this.today;
            this.classesList = [];
            this.handleCurrentChange(1)
        },
        //获取班次类型
        async getleaveType(){
            let {code,data} = await this.$Http.getleaveType();
            if(code){
                this.leaveTypeList = data;
            }
        },
        //获取班次
        async staffshiftType(){
            if(!this.paging.store_id)return;
            let {code,data} = await this.$Http.staffshiftType({
                store_id:this.paging.store_id,
            });
            if(code){
                this.classesList = data;
            }
        },
        // 获取岗位
        async homegetPosition() {
            let { code, data } = await this.$Http.homegetPosition();
            if (code) {
                this.positionList = data;
            }
        },
        async attendanceStatistics(){
            
        },
        async attendanceStatisticsMonth(){
            this.loading = true;
            let {code,data} = await this.$Http.attendanceStatisticsMonth(this.paging);
            this.loading = false;
            if(code){
                this.monthList = data.data;
                this.paging.total = data.total;
            }
        },
        async homegetStore(){
            let {code,data} = await this.$Http.homegetStore();
            if(code){
                this.shopList = data;
            }
        },
        handleCurrentChange(val){
            this.paging.page = val;
            this.getList()
        },
        async getList(){
            this.loading = true;
            let obj = {...this.paging};
            if(this.statistics){
                var {code,data} = await this.$Http.attendanceStatisticsMonth(obj);
                console.log(data,'data');
            }else {
                if( obj.start_time){
                    obj.end_time = obj.start_time[1];
                    obj.start_time = obj.start_time[0];
                }
                var {code,data} = await this.$Http.attendanceStatistics(obj);
            }
            this.loading = false;
            if(code){
                if(this.statistics)this.monthList = data;
                else this.dayList = data.data;
                this.paging.total = data.total;
            }
        },
        pickerDate(){

        },
        change(){

        }
    },
};
</script>

<style lang="scss" scoped>
::v-deep{
    .el-table{
        overflow: inherit;
        td{
            padding: 0;
        }
    }
    .el-table__header-wrapper{
        position: sticky;
        top: 0;
        width: 100%;
        z-index: 99;
    }
    .el-table__body-wrapper{
        height: 80vh;
        overflow: auto;
    }
}
.statistics{
    padding-bottom:20px;
    display: flex;
    justify-content: center;
}
.image-thumbnail{
    width: 130px;
    height: 130px;
}
</style>